package loader 
{
	import events.MyEvent;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;

	/**
	 * ...
	 * @author FireKnight
	 */
	public class SingelPicLoadedHaveContainer extends EventDispatcher 
	{
		protected var myLoader:Loader;
		protected var callBack:Function;
		protected var url:String;
		protected var container:Sprite;
		protected var para:*;
		public function SingelPicLoadedHaveContainer(s:String,b:Function,c:Sprite,_para:*,target:IEventDispatcher=null) 
		{
			super(target);
			callBack = b;
			url = s;
			container = c;
			para = _para;
			loadFile(s);
			
		}
		
		protected function removeLoader():void
		{
			if (myLoader)
			{
				myLoader = null;
			}
		}
		
		protected function handleIOERROR(e:IOErrorEvent):void
		{
			trace("handleIOERROR");
			trace(url);
		}
		
		protected function loadFile(s:String):void
		{
			removeLoader();
			myLoader = new Loader();
			myLoader.load(new URLRequest(s));
			myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			myLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, handleIOERROR);
		}
		
		protected function onComplete(e:Event):void
		{
			var bm:Bitmap = e.target.content as Bitmap;
			if (bm == null)
			{
				container.addChild(e.target.content);
			}
			else
			{
				bm.smoothing = true;
				container.addChild(bm);
			}
			
			if (callBack != null)
			{
				this.callBack(container,para);
			}
			this.dispatchEvent(new MyEvent("loaded",this))
			
			e.target.loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
			e.target.loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, handleIOERROR);
			removeLoader();
		}
		
	}

}